package edu.whut.mall.admin.service.impl;

import cn.hutool.core.bean.BeanUtil;
import com.github.pagehelper.PageHelper;
import edu.whut.mall.admin.domain.InstitutionParams;
import edu.whut.mall.admin.domain.UserParams;
import edu.whut.mall.admin.enums.SysStatusEnum;
import edu.whut.mall.admin.mapper.UserCustomMapper;
import edu.whut.mall.admin.service.IInstitutionService;
import edu.whut.mall.admin.service.ISysUserService;
import edu.whut.mall.admin.vo.UserVo;
import edu.whut.mall.common.api.CommonPage;
import edu.whut.mall.mapper.InstitutionMapper;
import edu.whut.mall.mapper.InstitutionSysUserMapper;
import edu.whut.mall.model.Institution;
import edu.whut.mall.model.InstitutionExample;
import edu.whut.mall.model.InstitutionSysUser;
import edu.whut.mall.model.SysUser;
import lombok.RequiredArgsConstructor;
import org.springframework.stereotype.Service;

import java.util.List;

/**
 * @program: d9lab-mall
 * @description: 机构服务实现类
 * @author: Wayne
 * @create: 2020-04-27 14:25
 **/
@Service
@RequiredArgsConstructor
public class InstitutionServiceImpl implements IInstitutionService {

    private final InstitutionMapper institutionMapper;

    private final ISysUserService sysUserService;

    private final InstitutionSysUserMapper institutionSysUserMapper;

    private final UserCustomMapper userCustomMapper;



    @Override
    public int insertOneInstitution(InstitutionParams institutionParams) {
        Institution institution=new Institution();
        BeanUtil.copyProperties(institutionParams, institution);
        institution.setStatus(SysStatusEnum.AVAILABLE.getCode());
        return institutionMapper.insertSelective(institution);
    }

    @Override
    public int assignAdmin2Institution(Long institutionId, String regIp, UserParams userParams) {
        //前提是调用这个方法传递的userType是商户管理员（1 要和前面区分开来
        SysUser sysUser=sysUserService.insertOneUser(regIp, userParams);
        InstitutionSysUser institutionSysUser = new InstitutionSysUser();
        institutionSysUser.setInstitutionId(institutionId);
        institutionSysUser.setSysUserId(sysUser.getId());
        return institutionSysUserMapper.insertSelective(institutionSysUser);
    }

    @Override
    public int updateOneInstitution(InstitutionParams institutionParams) {
        Institution institution = new Institution();
        BeanUtil.copyProperties(institutionParams, institution);
        return institutionMapper.updateByPrimaryKeySelective(institution);
    }



    @Override
    public List<UserVo> listUserVosByInstitutionId(Long institutionId) {
        return userCustomMapper.listUserVosByInstitutionId(institutionId);
    }

    @Override
    public CommonPage<Institution> listInstitutionVos(Integer pageNum, Integer size) {
        PageHelper.startPage(pageNum,size);
        //todo 这里以后会转换成institutionVo,但目前还不知道需要哪些数据，所以直接从数据库查
        List<Institution> institutionList = institutionMapper.selectByExample(new InstitutionExample());
        return CommonPage.restPage(institutionList);
    }
}
